<?php
    class MyDateTime{
        
        
        public static function relative($inputTime, $lang = 'vi') {
            if(!is_numeric($inputTime)) $inputTime = strtotime($inputTime); 

            $langConfig = array(
                'vi' => array(
                    'format' => 'd/m/Y',
                    'at' => 'lúc',
                    'moment' => 'Vừa mới đăng',
                    'minute_ago' => 'phút trước',
                    'hour' => 'giờ',
                    'today' => 'Hôm nay',
                    'yesterday' => 'Hôm qua',
                    'before_yesterday' => 'Hôm kia',
                ),
                'en' => array(
                    'format' => 'm/d/Y',
                    'at' => 'at',
                    'moment' => 'Just a moment',
                    'minute_ago' => 'minutes ago',
                    'hour' => 'hours',
                    'today' => 'Today',
                    'yesterday' => 'Yesterday', 
                ),
            );

            $dateInput = date('Y-m-d', $inputTime);

            $currentDatetime = self::getCurrentTime();
            $currentTime = strtotime($currentDatetime);

            $seconds = $currentTime - $inputTime; // Calculates time difference in seconds.
            if($seconds < 180){
                $s = $langConfig[$lang]['moment'];
            }elseif($seconds > 180 && $seconds < 3600){
                $minutes = floor($seconds / 60);
                $s = $minutes.' '.$langConfig[$lang]['minute_ago'];
            }elseif($seconds > 3600 && $seconds < 3600*5){
                $minutes = floor($seconds / 60);
                $hours = floor($minutes / 60);
                $minutes_diff = $minutes - ($hours*60);
                $s = $hours.' '.$langConfig[$lang]['hour'].' '.$minutes_diff.' '.$langConfig[$lang]['minute_ago'];
            }else{
                $today = date('Y-m-d', $currentTime);
                $yesterday = date('Y-m-d', strtotime('yesterday')); 
                $before_yesterday = date('Y-m-d', strtotime('yesterday - 1 day'));

                $timestamp_before_week = strtotime('today - 1 week');

                $s = date($langConfig[$lang]['format'], $inputTime); 
                if($dateInput == $today){
                    $s = $langConfig[$lang]['today'];    
                }elseif($dateInput == $yesterday){
                    $s = $langConfig[$lang]['yesterday'];    
                }elseif($dateInput == $before_yesterday && !empty($langConfig[$lang]['before_yesterday'])) {
                    $s = $langConfig[$lang]['before_yesterday'];   
                }

                if($inputTime > $timestamp_before_week){
                    $hour = date('H:i', $inputTime);
                    $s = "{$s} {$langConfig[$lang]['at']} {$hour}";    
                }
            }

            return $s;
        }

        public static function datetimeFormat($date, $dateOnly = false, $lang = 'vi'){
            if($dateOnly){
                $format = $lang == 'vi' ? 'd/m/Y' : 'm/d/Y';
            }else{
                $format = $lang == 'vi' ? 'd/m/Y H:i' : 'H:i';
            }
            return date($format, is_numeric($date) ? $date : strtotime($date));   
        }
        
        public static function getCurrentTime($time = 'now', $format = true){
            $dateTime = new DateTime($time, new DateTimeZone('Asia/Ho_Chi_Minh')); 
            //$dateTime = new DateTime("now", new DateTimeZone('America/Los_Angeles'));
            //$dateTime = new DateTime("now");
            if($time == 'now' && !IS_LOCAL)  $dateTime->modify('-4 hour');
            return $format ? $dateTime->format("Y-m-d H:i:s") : $dateTime; 
        }
    }
?>